package com.example.yoho.mvp.view.fragment;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.example.lib_core.mvp.view.BaseFragment;
import com.example.yoho.R;
import com.example.yoho.di.component.DaggerShowComponent;
import com.example.yoho.di.moudles.ShowModules;
import com.example.yoho.domain.Domain_Url;
import com.example.yoho.mvp.contract.ShowContract;
import com.example.yoho.mvp.model.entity.ShowEntity;
import com.example.yoho.mvp.presenter.ShowPresenter;
import com.example.yoho.mvp.view.adapter.MyShowAdapter;
import com.example.yoho.utils.MyGlide;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.jwenfeng.library.pulltorefresh.view.CategoryHeadRefreshView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyShowFragment extends BaseFragment<ShowPresenter> implements ShowContract.View {


    private Banner show_banner;
    private List<String> list_banners=new ArrayList<>();
    private android.support.v7.widget.RecyclerView showRecyc;
    private List<ShowEntity.ValuesBean> list_recyc=new ArrayList<>();
    private MyShowAdapter adapter_show;
    private com.jwenfeng.library.pulltorefresh.PullToRefreshLayout showPull;
    private ImageView imageView_u,imageView_d;

    @Override
    public int bondLayout() {
        return R.layout.fragment_my_show;
    }

    @Override
    public void Inject() {
        DaggerShowComponent.builder().showModules(new ShowModules(this)).build().Inject(this);
    }

    @Override
    public void initView() {

        showRecyc = (RecyclerView) findviewByIds(R.id.show_recyc);
        showPull = (PullToRefreshLayout) findviewByIds(R.id.show_pull);
    }

    @Override
    public void initData() {
        mPresenter.reuqestShowAll();
        adapter_show=new MyShowAdapter(R.layout.show_item,list_recyc);
        showRecyc.setAdapter(adapter_show);
        showRecyc.setLayoutManager(new LinearLayoutManager(getContext()));
        showPull.setHeaderView(new CategoryHeadRefreshView(getContext()));
        myshowrefresh();
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.myshow_headview, null);
         show_banner=inflate.findViewById(R.id.show_banners);
          imageView_u=inflate.findViewById(R.id.myshow_head);
          imageView_d=inflate.findViewById(R.id.myshow_headimg);
         adapter_show.addHeaderView(inflate);

    }

    private void myshowrefresh() {
        showPull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                list_recyc.clear();
                adapter_show.notifyDataSetChanged();
                mPresenter.reuqestShowAll();

            }

            @Override
            public void loadMore() {
                mPresenter.reuqestShowAll();
            }
        });
    }

    @Override
    public void showView(ShowEntity entity) {
        List<ShowEntity.BannerBean> banner = entity.getBanner();
        for (int i = 0; i < banner.size(); i++) {
            list_banners.add(Domain_Url.IMAGE_URL+banner.get(i).getRecommend_url());
        }
        show_banner.setImages(list_banners);
        showbanners();
        show_banner.start();
        List<ShowEntity.ValuesBean> values = entity.getValues();
        list_recyc.addAll(values);
        adapter_show.notifyDataSetChanged();
        showPull.finishRefresh();
        showPull.finishLoadMore();
        MyGlide.GlideFor(getContext(),Domain_Url.IMAGE_URL+entity.getBanner().get(0).getRecommend_url(),imageView_u);
        MyGlide.GlideFor(getContext(),Domain_Url.IMAGE_URL+entity.getBanner().get(1).getRecommend_url(),imageView_d);

    }

    private void showbanners() {
        show_banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                MyGlide.GlideAS(context,path,imageView);
            }
        });
    }
}
